package com.example.zhihu.task;

import cn.hutool.core.thread.ExecutorBuilder;
import com.example.zhihu.entity.HotData.DataDTO;
import com.example.zhihu.entity.HotDataWeek;
import com.example.zhihu.entity.HotInfo;
import com.example.zhihu.entity.WeekHot;
import com.example.zhihu.service.HotDataService;
import com.example.zhihu.service.HotDataWeekService;
import com.example.zhihu.service.IHotInfoService;
import com.example.zhihu.service.IWeekHotService;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author t-zhaoheng
 * @since 2022/4/16
 */
@EnableScheduling
@Component
@Slf4j
public class ScryptTask {

  @Resource
  HotDataService hotDataService;

  @Resource
  IHotInfoService iHotInfoService;

  @Resource
  HotDataWeekService hotDataWeekService;

  @Resource
  IWeekHotService iWeekHotService;


//  @Scheduled(fixedDelay = 1000 * 60 * 60)
  // 每隔一个小时执行一次
//  @Scheduled(cron = "0 0 * * * ?")
  // 每隔10分钟执行一次
  @Scheduled(cron = "0 */10 * * * ?")
  public void getHotData() {
    log.info("开始爬取知乎热榜数据");
    // 从知乎爬原始热榜数据
    List<DataDTO> dataDTOS = hotDataService.getDataDTO();

    // 将抓包数据转化为自定义的对象HotInfo
    List<HotInfo> hotInfoList = dataDTOS.stream()
        .map(dataDTO -> hotDataService.convert2HotInfo(dataDTO)).collect(Collectors.toList());

    // 向hotInfoList填充关键词
    iHotInfoService.addKeyword(hotInfoList);

    // 将hostInfoList保存到数据库
    iHotInfoService.saveOrUpdateBatch(hotInfoList);
    log.info("知乎热榜数据已存入数据库中");

  }

}
